unit ClsCLFiscalNCM;

interface

uses uDMCI, DbTables, ubiblioCI, sysutils;

type
  TCLFiscalNCM = class(TObject)
  private
    CodF_codigo: string;
  public
    MNCM_DESCRICAO: string;
    MNCM_NF_CLASSFISCAL: string;
    MNCM_CODIGO: string;
    MNCM_DESCRICAO_TIPI : string;
    MNCM_EX_TIPI : string;
    function Gravar(P_MNCM_ID_NCM: Integer): Byte;
    function Excluir(P_MNCM_ID_NCM: Integer): Byte;
    procedure Consultar(var p_q: TQuery);
    function RetornaMaiorCodigo: Integer;
    procedure Consultar2(var p_q: TQuery; P_DESCRICAO: string);
    function StrZero(entrada: string; tamanho: integer): string;
  end;

implementation

{ TCodFiscal }

procedure TCLFiscalNCM.Consultar(var p_q: TQuery);
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add(' SELECT M.DESCRICAO, M.MNCM_NF_CLASSFISCAL, M.MNCM_ID_NCM, M.MNCM_DESCRICAO_TIPI, M.MNCM_CODIGO, M.MNCM_EXTIPI, '+
              '        M.MNCM_CODIGO||'+#39+' - '+#39+'||M.DESCRICAO AS  ClassNCM'+
              ' FROM MERCADORIA_NCM M ORDER BY M.MNCM_CODIGO');


end;

procedure TCLFiscalNCM.Consultar2(var p_q: TQuery; P_DESCRICAO: string);
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('SELECT * FROM MERCADORIA_NCM WHERE DESCRICAO LIKE (:P_DESCRICAO) ORDER BY DESCRICAO');
  p_q.parambyname('P_DESCRICAO').asstring := P_DESCRICAO;
end;

function TCLFiscalNCM.Excluir(P_MNCM_ID_NCM: Integer): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DMCI);
  tq.databasename := DMCI.dbCentralImpostos.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM MERCADORIA_NCM where MNCM_ID_NCM=:P_MNCM_ID_NCM');
  tq.parambyname('P_MNCM_ID_NCM').AsInteger := P_MNCM_ID_NCM;
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

function TCLFiscalNCM.Gravar(P_MNCM_ID_NCM: Integer): Byte;
var
  tq, tq1: TQuery;
begin
  tq := Tquery.Create(DMCI);
  tq.databasename := DMCI.dbCentralImpostos.databasename;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;

  tq1              := Tquery.Create(DMCI);
  tq1.databasename := DMCI.dbCentralImpostos.databasename;
  tq1.active       := False;
  tq1.sql.clear;
  tq1.params.clear;

  tq.sql.add('SELECT * FROM MERCADORIA_NCM where MNCM_ID_NCM=:P_MNCM_CODIGO');
  tq.parambyname('P_MNCM_CODIGO').AsInteger := P_MNCM_ID_NCM;
  tq.open;
  if tq.eof then begin
    tq1.close;
    tq1.sql.clear;
    tq1.params.clear;
    tq1.sql.add(' INSERT INTO MERCADORIA_NCM  (DESCRICAO,MNCM_NF_CLASSFISCAL,MNCM_ID_NCM,MNCM_DESCRICAO_TIPI,MNCM_CODIGO, MNCM_EXTIPI) '+
               ' VALUES(:P_MNCM_DESCRICAO,:P_MNCM_NF_CLASSFISCAL,:P_MNCM_ID_NCM,:P_MNCM_DESCRICAO_TIPI,:P_MNCM_CODIGO, :P_MNCM_EX_TIPI) ');
  end else begin
    tq1.close;
    tq1.sql.clear;
    tq1.params.clear;
    tq1.sql.add(' UPDATE MERCADORIA_NCM SET DESCRICAO = :P_MNCM_DESCRICAO,MNCM_NF_CLASSFISCAL = :P_MNCM_NF_CLASSFISCAL, MNCM_DESCRICAO_TIPI = :P_MNCM_DESCRICAO_TIPI, MNCM_CODIGO = :P_MNCM_CODIGO, MNCM_EXTIPI = :P_MNCM_EX_TIPI WHERE MNCM_ID_NCM = :P_MNCM_ID_NCM ');
  end;
  tq1.parambyname('P_MNCM_DESCRICAO').AsString      := trim(MNCM_DESCRICAO);
  tq1.parambyname('P_MNCM_NF_CLASSFISCAL').AsString := MNCM_NF_CLASSFISCAL;
  tq1.parambyname('P_MNCM_ID_NCM').AsInteger        := P_MNCM_ID_NCM;
  tq1.ParamByName('P_MNCM_CODIGO').AsString         := MNCM_CODIGO;
  tq1.ParamByName('P_MNCM_DESCRICAO_TIPI').AsString := MNCM_DESCRICAO_TIPI;
  tq1.ParamByName('P_MNCM_EX_TIPI').AsString        := MNCM_EX_TIPI;
  tq1.execsql;
  tq1.close;
  tq1.Free;
  result := 1;
  tq.close;
  tq.Free;
end;

function TCLFiscalNCM.RetornaMaiorCodigo(): Integer;
var
  tq1: TQuery;
begin
  tq1 := Tquery.Create(DMCI);
  tq1.databasename := DMCI.dbCentralImpostos.databasename;
  tq1.active := False;
  tq1.close;
  tq1.sql.Clear;
  tq1.params.clear;
  tq1.sql.add('SELECT MAX(MNCM_ID_NCM) AS MAIORCODIGO FROM MERCADORIA_NCM ');
  tq1.Open;
  if trim(tq1.FieldByName('MAIORCODIGO').AsString) = '' then
    Result := 0
  else
    Result := tq1.FieldByName('MAIORCODIGO').AsInteger;
  tq1.close;
  tq1.Free;
end;

function TCLFiscalNCM.StrZero(entrada: string; tamanho: integer): string;
var
  retorno_str_zero: string;
  tamanho_str_entrada: integer;
  contador: integer;

begin
  tamanho_str_entrada := length(entrada);

  retorno_str_zero := '';
  for contador := 1 to tamanho - tamanho_str_entrada do begin
    retorno_str_zero := retorno_str_zero + '0';
  end;
  retorno_str_zero := retorno_str_zero + entrada;

  Result := retorno_str_zero;
end;

end.
